import { customRef } from "vue";
export default function (init: string, delay: number) {
  /* 自定义ref */
  let timer: number;
  let msg = customRef((track, trigger) => {
    return {
      get() {
        console.log("get");
        track(); //vue 对msg变化持续关注，变化更新
        return init;
      },
      set(value) {
        console.log("set", value);
        init = value;
        clearTimeout(timer);
        timer = setTimeout(() => {
          trigger(); //通知vue数据变化了
        }, delay);
      },
    };
  });
  return {msg}
}
